<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="网格节点价位" prop="gridNodePrice">
        <el-input
          v-model="queryParams.gridNodePrice"
          placeholder="请输入网格节点价位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际成交价" prop="actualTradePrice">
        <el-input
          v-model="queryParams.actualTradePrice"
          placeholder="请输入实际成交价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="网格价格" prop="gridPrice">
        <el-input
          v-model="queryParams.gridPrice"
          placeholder="请输入网格价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手续费" prop="fee">
        <el-input
          v-model="queryParams.fee"
          placeholder="请输入手续费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证券持仓数量" prop="securityHoldingQuantity">
        <el-input
          v-model="queryParams.securityHoldingQuantity"
          placeholder="请输入证券持仓数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成本价" prop="costBasis">
        <el-input
          v-model="queryParams.costBasis"
          placeholder="请输入成本价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用资金数量" prop="availableCashBalance">
        <el-input
          v-model="queryParams.availableCashBalance"
          placeholder="请输入可用资金数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证券当前价格" prop="currentSecurityPrice">
        <el-input
          v-model="queryParams.currentSecurityPrice"
          placeholder="请输入证券当前价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合约价格" prop="contractPrice">
        <el-input
          v-model="queryParams.contractPrice"
          placeholder="请输入合约价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合约建仓年份" prop="contractInitiationYear">
        <el-input
          v-model="queryParams.contractInitiationYear"
          placeholder="请输入合约建仓年份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合约名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合约名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合约数量" prop="contractQuantity">
        <el-input
          v-model="queryParams.contractQuantity"
          placeholder="请输入合约数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="optionType">
        <el-select v-model="queryParams.optionType" placeholder="请选择类别" style="width: 100%">
          <el-option label="认购" :value="0" />
          <el-option label="认沽" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="波动范围" prop="fluctuationRange">
        <el-input
          v-model="queryParams.fluctuationRange"
          placeholder="请输入波动范围"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公差" prop="commonDifference">
        <el-input
          v-model="queryParams.commonDifference"
          placeholder="请输入公差"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="做空锁定数量" prop="shortQuantity">
        <el-input
          v-model="queryParams.shortQuantity"
          placeholder="请输入做空锁定数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投资信息id" prop="investmentId">
        <el-input
          v-model="queryParams.investmentId"
          placeholder="请输入投资信息id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投资人姓名" prop="investorName">
        <el-input
          v-model="queryParams.investorName"
          placeholder="请输入投资人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="独立运营标识" prop="independentFlag">
        <el-select v-model="queryParams.independentFlag" placeholder="请选择独立运营标识" style="width: 100%">
          <el-option label="非独立" :value="0" />
          <el-option label="独立" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="独立运营组" prop="independentGroup">
        <el-input
          v-model="queryParams.independentGroup"
          placeholder="请输入独立运营组"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计算标识" prop="calculateFlag">
        <el-select v-model="queryParams.calculateFlag" placeholder="请选择计算标识" style="width: 100%">
          <el-option label="未计算" :value="0" />
          <el-option label="已计算" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['trade:costGrid:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['trade:costGrid:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['trade:costGrid:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['trade:costGrid:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="costGridList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="唯一标识ID" align="center" prop="id" />
      <el-table-column label="网格节点价位" align="center" prop="gridNodePrice" />
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="实际成交价" align="center" prop="actualTradePrice" />
      <el-table-column label="网格成本价格" align="center" prop="gridPrice" />
      <el-table-column label="手续费" align="center" prop="fee" />
      <el-table-column label="证券持仓数量" align="center" prop="securityHoldingQuantity" />
      <el-table-column label="成本价" align="center" prop="costBasis" />
      <el-table-column label="可用资金数量" align="center" prop="availableCashBalance" />
      <el-table-column label="证券当前价格" align="center" prop="currentSecurityPrice" />
      <el-table-column label="合约价格" align="center" prop="contractPrice" />
      <el-table-column label="合约建仓年份" align="center" prop="contractInitiationYear" />
      <el-table-column label="合约名称" align="center" prop="contractName" />
      <el-table-column label="合约数量" align="center" prop="contractQuantity" />
      <el-table-column label="类别" align="center" prop="optionType">
        <template slot-scope="scope">
          <span v-if="scope.row.optionType === 0">认购</span>
          <span v-else-if="scope.row.optionType === 1">认沽</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="波动范围" align="center" prop="fluctuationRange" />
      <el-table-column label="公差" align="center" prop="commonDifference" />
      <el-table-column label="做空锁定数量" align="center" prop="shortQuantity" />
      <el-table-column label="投资信息id" align="center" prop="investmentId" />
      <el-table-column label="投资人姓名" align="center" prop="investorName" />
      <el-table-column label="独立运营标识" align="center" prop="independentFlag">
        <template slot-scope="scope">
          <span v-if="scope.row.independentFlag === 0">非独立</span>
          <span v-else-if="scope.row.independentFlag === 1">独立</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="独立运营组" align="center" prop="independentGroup" />
      <el-table-column label="计算标识" align="center" prop="calculateFlag">
        <template slot-scope="scope">
          <span v-if="scope.row.calculateFlag === 0">未计算</span>
          <span v-else-if="scope.row.calculateFlag === 1">已计算</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['trade:costGrid:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['trade:costGrid:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改成本网格对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="网格节点价位" prop="gridNodePrice">
          <el-input v-model="form.gridNodePrice" placeholder="请输入网格节点价位例如：3100" />
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量例如：7或8" />
        </el-form-item>
        <el-form-item label="实际成交价" prop="actualTradePrice">
          <el-input v-model="form.actualTradePrice" placeholder="请输入实际成交价例如：31000" />
        </el-form-item>
        <el-form-item label="网格价格" prop="gridPrice">
          <el-input v-model="form.gridPrice" placeholder="请输入网格价格例如：31100" />
        </el-form-item>
        <el-form-item label="手续费" prop="fee">
          <el-input v-model="form.fee" placeholder="请输入手续费例如：5" />
        </el-form-item>
        <el-form-item label="证券持仓数量" prop="securityHoldingQuantity">
          <el-input v-model="form.securityHoldingQuantity" placeholder="请输入证券持仓数量" />
        </el-form-item>
        <el-form-item label="成本价" prop="costBasis">
          <el-input v-model="form.costBasis" placeholder="请输入成本价" />
        </el-form-item>
        <el-form-item label="可用资金数量" prop="availableCashBalance">
          <el-input v-model="form.availableCashBalance" placeholder="请输入可用资金数量" />
        </el-form-item>
        <el-form-item label="证券当前价格" prop="currentSecurityPrice">
          <el-input v-model="form.currentSecurityPrice" placeholder="请输入证券当前价格" />
        </el-form-item>
        <el-form-item label="合约价格" prop="contractPrice">
          <el-input v-model="form.contractPrice" placeholder="请输入合约价格" />
        </el-form-item>
        <el-form-item label="合约建仓年份" prop="contractInitiationYear">
          <el-input v-model="form.contractInitiationYear" placeholder="请输入合约建仓年份例如：2025" />
        </el-form-item>
        <el-form-item label="合约名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合约名称" />
        </el-form-item>
        <el-form-item label="合约数量" prop="contractQuantity">
          <el-input v-model="form.contractQuantity" placeholder="请输入合约数量" />
        </el-form-item>
        <el-form-item label="类别" prop="optionType">
          <el-select v-model="form.optionType" placeholder="请选择类别" style="width: 100%">
            <el-option label="认购" :value="0" />
            <el-option label="认沽" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="波动范围" prop="fluctuationRange">
          <el-input v-model="form.fluctuationRange" placeholder="请输入波动范围例如：1" />
        </el-form-item>
        <el-form-item label="公差" prop="commonDifference">
          <el-input v-model="form.commonDifference" placeholder="请输入公差例如：10" />
        </el-form-item>
        <el-form-item label="做空锁定数量" prop="shortQuantity">
          <el-input v-model="form.shortQuantity" placeholder="请输入做空锁定数量" />
        </el-form-item>
        <el-form-item label="投资信息id" prop="investmentId">
          <el-input v-model="form.investmentId" placeholder="请输入投资信息id" />
        </el-form-item>
        <el-form-item label="投资人姓名" prop="investorName">
          <el-input v-model="form.investorName" placeholder="请输入投资人姓名" />
        </el-form-item>
        <el-form-item label="独立运营标识" prop="independentFlag">
          <el-select v-model="form.independentFlag" placeholder="请选择独立运营标识" style="width: 100%">
            <el-option label="非独立" :value="0" />
            <el-option label="独立" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item label="独立运营组" prop="independentGroup">
          <el-input v-model="form.independentGroup" placeholder="请输入独立运营组" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCostGrid, getCostGrid, delCostGrid, addCostGrid, updateCostGrid } from "@/api/trade/costGrid"

export default {
  name: "CostGrid",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成本网格表格数据
      costGridList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gridNodePrice: null,
        quantity: null,
        actualTradePrice: null,
        gridPrice: null,
        fee: null,
        securityHoldingQuantity: null,
        costBasis: null,
        availableCashBalance: null,
        currentSecurityPrice: null,
        contractPrice: null,
        contractName: null,
        contractQuantity: null,
        optionType: null,
        fluctuationRange: null,
        commonDifference: null,
        shortQuantity: null,
        contractInitiationYear: null,
        investmentId: null,
        investorName: null,
        independentFlag: null,
        independentGroup: null,
        calculateFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询成本网格列表 */
    getList() {
      this.loading = true
      listCostGrid(this.queryParams).then(response => {
        this.costGridList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gridNodePrice: null,
        quantity: null,
        actualTradePrice: null,
        gridPrice: null,
        fee: null,
        securityHoldingQuantity: null,
        costBasis: null,
        availableCashBalance: null,
        currentSecurityPrice: null,
        contractPrice: null,
        contractName: null,
        contractQuantity: null,
        optionType: null,
        fluctuationRange: null,
        commonDifference: null,
        shortQuantity: null,
        contractInitiationYear: null,
        investmentId: null,
        investorName: null,
        independentFlag: null,
        independentGroup: null,
        calculateFlag: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加成本网格"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getCostGrid(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改成本网格"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCostGrid(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCostGrid(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除成本网格编号为"' + ids + '"的数据项？').then(function() {
        return delCostGrid(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('trade/costGrid/export', {
        ...this.queryParams
      }, `costGrid_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
